import java.io.*;
import java.util.ArrayList;

public class Service {
  //对这个集合进行 增删 改查操作
  private ArrayList<Student> students = null;

  //每次运行java程序就直接读取就好了
  public Service() {
    //以防万一
    if (new File("src/student.data").exists()) {
      try {
        ObjectInputStream objectInputStream =
            new ObjectInputStream(new FileInputStream("src//student.data"));
        students = (ArrayList<Student>) objectInputStream.readObject();

        objectInputStream.close();
      } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
        students = new ArrayList<Student>();//以防万一
        throw new RuntimeException("文件读取失败");
      }

    } else {//不存在再创建一个文件
      try {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("庄周", 100.0, 100.0, 100.0));
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("src//student.data"));
        objectOutputStream.writeObject(students);
        objectOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();

        students = new ArrayList<Student>();//以防万一
        throw new RuntimeException("文件创建失败");

      }
    }


  }

  // 重要方法 每次dml操作立马更新数据
  public void update() {
    //每次覆盖更新
    try {
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("src//student.data"));
      objectOutputStream.writeObject(this.students);
      objectOutputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("更新失败");

    }
  }

  public boolean add(Student student) {

    if (students.add(student)) {
      update();//更新数据
      return true;
    }
    return false;
  }

  //查找学生 id和姓名都可以
  public Student find(String str) {
    String snoRegx = "\\d+";//学号
    String nameRegx = "[\\u4e00-\\u9fa5A-Za-z]+";//姓名
//判断str 是学号吗
    if (str.matches(snoRegx)) {//根据学号删除
      for (Student student : students) {
        //学号是否在这个集合存在 在就返回
        if (student.getSno() == Integer.parseInt(str)) {
          return student;
        }
      }
    }
    //判断str 是姓名吗
    if (str.matches(nameRegx)) {
      //遍历找到该对象
      for (Student student : students) {
        if (student.getName().equals(str)) {
          return student;
        }
      }
    }
    return null;
  }

  public boolean remove(String str) {
    String snoRegx = "\\d+";//学号
    String nameRegx = "[\\u4e00-\\u9fa5A-Za-z]+";//姓名
    boolean result = false;//是否删除成功
//判断str 是学号吗
    if (str.matches(snoRegx)) {//根据学号删除
      for (Student student : students) {
        if (student.getSno() == Integer.parseInt(str)) {
          result = students.remove(student);//删除成功

          update();//更新文件
          return result;
        }
      }
    }
    //判断str 是姓名吗
    if (str.matches(nameRegx)) {
      for (Student student : students) {
        if (student.getName().equals(str)) {

          result = students.remove(student);//删除成功

          update();//更新文件
          return result;
        }
      }
    }
    return result;
  }


  public ArrayList<Student> getStudents() {
    return students;
  }

}
